package com.baihan.angus.app.game.busshow.adps;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.BaseApplication;
import com.baihan.angus.app.game.basic.base.BaseAdp;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.DmPropsResponse;
import com.baihan.angus.app.game.common.util.ImageLoader;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

public class DmPropsAdp extends BaseAdp<DmPropsResponse.DataX.Data, DmPropsAdp.ViewHolder> {

    private OnItemClickListener mOnItemClickListener;

    @Override
    protected RecyclerView.ViewHolder createYourViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dm_props, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void bindYourViewHolder(@NonNull ViewHolder holder, int position) {
        ViewHolder vh = holder;

        DmPropsResponse.DataX.Data item = getItem(position);

        vh.tvTitle.setText(item.getTitle());

        ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                vh.ivProps, item.getUrl().get(0).getUrl(), R.mipmap.ic_test_bg, 0);

        if (item.getIs_send() == 0) {//0-未发送 1-已发送
            vh.tvSend.setEnabled(true);
        } else {
            vh.tvSend.setEnabled(false);
        }

        if (!TextUtils.isEmpty(item.getScript_role_name())) {
            vh.tvSendRole.setVisibility(View.VISIBLE);
            vh.tvSendRole.setText("发送：" + item.getScript_role_name());
        } else {
            vh.tvSendRole.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(item.getHold_script_role_name())) {
            vh.tvHoldRole.setVisibility(View.VISIBLE);
            vh.tvHoldRole.setText("持有：" + item.getHold_script_role_name());
        } else {
            vh.tvHoldRole.setVisibility(View.GONE);
        }
        if (item.getIs_use() == 0) {//0-未使用 1-已使用
            vh.tvIsUsed.setText("状态：未使用");
        } else if (item.getIs_use() == 1) {
            vh.tvIsUsed.setText("状态：已使用");
        }

        ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                vh.ivProps, item.getUrl().get(0).getUrl(), R.mipmap.ic_test_bg, 0);

        vh.tvSend.setOnClickListener(v -> {
            mOnItemClickListener.onSend(position);
        });

        vh.flContent.setOnClickListener(v -> {
            if (vh.ivProps.getVisibility() == View.VISIBLE) {
                vh.ivProps.setVisibility(View.GONE);
                vh.llDetail.setVisibility(View.VISIBLE);
            } else if (vh.llDetail.getVisibility() == View.VISIBLE) {
                vh.ivProps.setVisibility(View.VISIBLE);
                vh.llDetail.setVisibility(View.GONE);
            }
        });
    }

    @Override
    protected int getEmptyView() {
        return 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle, tvSend, tvSendRole, tvHoldRole, tvIsUsed;
        QMUIRadiusImageView ivProps;
        LinearLayout llDetail;
        FrameLayout flContent;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvSend = itemView.findViewById(R.id.tvSend);
            tvSendRole = itemView.findViewById(R.id.tvSendRole);
            tvHoldRole = itemView.findViewById(R.id.tvHoldRole);
            tvIsUsed = itemView.findViewById(R.id.tvIsUsed);
            ivProps = itemView.findViewById(R.id.ivProps);
            llDetail = itemView.findViewById(R.id.llDetail);
            flContent = itemView.findViewById(R.id.flContent);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onSend(int position);
    }
}
